package com.test.deencredit.util_deencredit.http_deencredit;

import static com.test.deencredit.config_deencredit.Paths_deencredit.PATH_UPLOAD_IMAGE_FILE_deencredit;
import static com.test.deencredit.util_deencredit.AESCryptUtil_deencredit.decryptDefault_deencredit;
import static com.test.deencredit.util_deencredit.AESCryptUtil_deencredit.encryptDefault_deencredit;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.test.deencredit.analytics_deencredit.event_deencredit.IAnalyticsEvent_deencredit;
import com.test.deencredit.analytics_deencredit.util_deencredit.AnalyticsUtil_deencredit;
import com.test.deencredit.R;
import com.test.deencredit.base_deencredit.BaseApplication_deencredit;
import com.test.deencredit.bean_deencredit.config_deencredit.AppShowCustomize_deencredit;
import com.test.deencredit.bean_deencredit.process_deencredit.ProcessInfo_deencredit;
import com.test.deencredit.bean_deencredit.star_dialog_deencredit.StarWindowConfig_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.AccountList_Fr_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.LnAplyDetailInfo_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.LnLimitInfo_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.LoginInfo_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.OthersInfo_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.RpInfo_deencredit;
import com.test.deencredit.config_deencredit.Constant_deencredit;
import com.test.deencredit.config_deencredit.CountryConfig_deencredit;
import com.test.deencredit.config_deencredit.Paths_deencredit;
import com.test.deencredit.dialog_deencredit.LoadingDialog_deencredit;
import com.test.deencredit.util_deencredit.UserInfoUtil_deencredit;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;


public class HttpUtil_deencredit {

    public interface ResponseListener_deencredit {
        void onResponse(JSONObject responseJson);
    }

    private static OkHttpClient mClient_deencredit;

    public LoadingDialog_deencredit progressBarDialog_deencredit;

    private static final HttpLoggingInterceptor mHttpLoggingInterceptor_deencredit = new HttpLoggingInterceptor(new HttpLogger_deencredit());

    private Handler mHandle_deencredit;

    private HttpUtil_deencredit() {

        //tag2024
        mHttpLoggingInterceptor_deencredit.setLevel(HttpLoggingInterceptor.Level.BODY);

        mClient_deencredit = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .addNetworkInterceptor(mHttpLoggingInterceptor_deencredit)
                .addInterceptor(new ReqInterceptor_deencredit())
                .callTimeout(5000, TimeUnit.MILLISECONDS)
                .build();

        mHandle_deencredit = new Handler();
    }

    private static class Inner_deencredit {
        private static final HttpUtil_deencredit instance_deencredit = new HttpUtil_deencredit();
    }

    public static HttpUtil_deencredit getInstance_deencredit() {
        return Inner_deencredit.instance_deencredit;
    }

    public void safeShowLoadingDialog_deencredit(FragmentActivity activity) {
        if (activity == null) return;
        safeRunUi_deencredit(() -> {
            progressBarDialog_deencredit = new LoadingDialog_deencredit(activity);
            progressBarDialog_deencredit.show();
        });
    }

    public void safeDismissLoadingDialog_deencredit() {
        if (progressBarDialog_deencredit !=null && progressBarDialog_deencredit.isShowing()) safeRunUi_deencredit(() -> progressBarDialog_deencredit.dismiss());
    }

    public void safeShowToast_deencredit(CharSequence text) {
        safeRunUi_deencredit(() -> Toast.makeText(BaseApplication_deencredit.getInstant(),text,Toast.LENGTH_SHORT).show());
    }

    private void safeRunUi_deencredit(Runnable runnable){
        if (Looper.myLooper() == Looper.getMainLooper()) runnable.run();
        else mHandle_deencredit.post(runnable);
    }

    //发送请求（带加载弹窗）
    private void requestWithLoadingDialog_deencredit(FragmentActivity activity, String body, ResponseListener_deencredit responseListener) {
        if (CountryConfig_deencredit.getInstance_deencredit().isFrontEmpty_deencredit()) return;
        safeShowLoadingDialog_deencredit(activity);
        request_deencredit(body,responseListener);
    }

    //发送请求(不带加载弹窗)
    private void request_deencredit(String body, ResponseListener_deencredit responseListener) {
        if (CountryConfig_deencredit.getInstance_deencredit().isFrontEmpty_deencredit()) return;
        String commonPath = CountryConfig_deencredit.getInstance_deencredit().getCommonPath_deencredit();
        if (TextUtils.isEmpty(commonPath)) return;
        new Thread(() -> {

            Request request = new Request.Builder()
                    .url(commonPath)
                    .header("product", Constant_deencredit.PRODUCT_deencredit)
                    .post(RequestBody.Companion.create(body, MediaType.Companion.parse("*/*;charset=utf-8")))
                    .build();

            mClient_deencredit.newCall(request).enqueue(
                    //响应
                    new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_request_fail_deencredit));
                            safeDismissLoadingDialog_deencredit();
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            String body_src = response.body().string();
                            new Thread(() -> {
                                String body = decryptDefault_deencredit(body_src);
                                if (responseListener!=null) {
                                    try {
                                        JSONObject responseJson = new JSONObject(body);
                                        responseListener.onResponse(responseJson);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                        safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_request_fail_deencredit));
                                        safeDismissLoadingDialog_deencredit();
                                    }
                                }
                            }).start();
                        }
                    }
            );

        }).start();
    }

    private String getRequestBody_deencredit(String path, JSONObject jsonObject) {
        if (jsonObject==null) jsonObject = new JSONObject();
        path = path.replaceAll("/", "");
        try {
            jsonObject.put("path", path);
            jsonObject.put("isLimitAdTrackingEnabled", AnalyticsUtil_deencredit.INSTANCE.getAdEnabled_deencredit());
            jsonObject.put("aduid", AnalyticsUtil_deencredit.INSTANCE.getAdId_deencredit());
            jsonObject.put("aduidPath", AnalyticsUtil_deencredit.INSTANCE.getAndroidId_deencredit());
            jsonObject.put("version", Constant_deencredit.APP_VERSION_deencredit.trim());
            jsonObject.put("token", UserInfoUtil_deencredit.getInstance_deencredit().getToken_deencredit().trim());
            jsonObject.put("androidId", AnalyticsUtil_deencredit.INSTANCE.getAndroidId_deencredit());
            jsonObject.put("userId", UserInfoUtil_deencredit.getInstance_deencredit().getUserId_deencredit().trim());
            jsonObject.put("advertId", AnalyticsUtil_deencredit.INSTANCE.getAdId_deencredit());
            jsonObject.put("product", Constant_deencredit.PRODUCT_deencredit.trim());
            jsonObject.put("clientType", Constant_deencredit.CLIENT_deencredit.trim());
            jsonObject.put("apiVersion", "v3");
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }
        return encryptDefault_deencredit(jsonObject.toString());
    }

    //获取App配置
    public void requestAppSInfo_deencredit(FragmentActivity activity, Runnable respRunnable) {
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_APP_SHOW_INFO_deencredit, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            AppShowCustomize_deencredit.parse_deencredit(((JSONObject)obj).optString("customize"));
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable != null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                });
    }

    //获取登录验证码
    public void requestOtpCode_deencredit(FragmentActivity activity, String phoneNum, Runnable respRunnable) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", phoneNum.trim());
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }

        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_SMS_CODE_deencredit, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                       else if (obj instanceof JSONObject) {
                            BaseApplication_deencredit.getInstant().setMsg_deencredit(msg);
                            OthersInfo_deencredit.getInstance_deencredit().setSendOtpCodeJson_deencredit((JSONObject) obj);
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable != null) safeRunUi_deencredit(respRunnable);
                       }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //注册登录-有验证码
    public void userLg_deencredit(FragmentActivity activity, String phoneNum, String verCode, Runnable respRunnable) {
        UserInfoUtil_deencredit.getInstance_deencredit().setMobile_deencredit(phoneNum);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("channelCode", AnalyticsUtil_deencredit.INSTANCE.getInstallReferrer_deencredit());
            jsonObject.put("subChannelCode", AnalyticsUtil_deencredit.INSTANCE.getInstallReferrer_deencredit());
            jsonObject.put("code", verCode.trim());
            jsonObject.put("mobile", phoneNum.trim());
            jsonObject.put("notBindWalletFlag", "YES");
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_USER_LOGIN_deencredit, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            AnalyticsUtil_deencredit.INSTANCE.initDevice_deencredit(BaseApplication_deencredit.getInstant());
                            LoginInfo_deencredit.parse_deencredit((JSONObject) obj);
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //注册登录-老用户
    public void userLoginOldUser_deencredit(FragmentActivity activity, String phoneNum, Runnable respRunnable) {
        UserInfoUtil_deencredit.getInstance_deencredit().setMobile_deencredit(phoneNum);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("channelCode", AnalyticsUtil_deencredit.INSTANCE.getInstallReferrer_deencredit());
            jsonObject.put("subChannelCode", AnalyticsUtil_deencredit.INSTANCE.getInstallReferrer_deencredit());
            jsonObject.put("code", "0000");
            jsonObject.put("mobile", phoneNum.trim());
            jsonObject.put("directLoginFlag","YES");
            jsonObject.put("notBindWalletFlag","YES");
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_USER_LOGIN_deencredit, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            AnalyticsUtil_deencredit.INSTANCE.initDevice_deencredit(BaseApplication_deencredit.getInstant());
                            LoginInfo_deencredit.parse_deencredit((JSONObject) obj);
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    public void submitFeedback_deencredit(FragmentActivity activity, String type, String opinion, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("type", type);
            jsonObject.put("opinion", opinion);
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_FEED_BACK_deencredit, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //请求用户流程状态
    public void userProcess_deencredit(FragmentActivity activity, Runnable respRunnable) {
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_USER_PROCESS_deencredit, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            ProcessInfo_deencredit.parse_deencredit((JSONObject) obj);
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //上传用户信息
    public void submitProcessInfo_deencredit(FragmentActivity activity, String path, TreeMap<String, String> dataMap, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            for (String k : dataMap.keySet()) {
                String value = dataMap.get(k);
                jsonObject.put(k, value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }

        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(path, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //请求额度列表
    public void requestLnLimit_deencredit(FragmentActivity activity, Runnable respRunnable) {
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_LOAN_LIMIT_deencredit, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            LnLimitInfo_deencredit.parse_deencredit((JSONObject) obj);
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //请求借款申请页信息/贷款详情
    public void requestLnAplyDetail_deencredit(FragmentActivity activity, Runnable respRunnable) {
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_GET_LOAN_APPLY_DETAIL_deencredit, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            LnAplyDetailInfo_deencredit.parse_deencredit((JSONObject) obj);
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //发起借款申请
    public void aply_deencredit(FragmentActivity activity, Runnable respRunnable) {
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_LOAN_APPLY_deencredit, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                            //成功响应
                        else if (obj instanceof JSONObject) {
                            AnalyticsUtil_deencredit.INSTANCE.addEvent_deencredit(IAnalyticsEvent_deencredit.AT_LOAN_APPLY);
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //获取还款信息
    public void requestRpDetail_deencredit(FragmentActivity activity, Runnable respRunnable) {
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_REPAY_INFO_deencredit, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            RpInfo_deencredit.parse((JSONObject) obj);
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //获取还款H5（加纳、科特）
    public void orderRepayH5_deencredit(FragmentActivity activity, String deferredFlag, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("deferredFlag", deferredFlag.trim());
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_ORDER_REPAY_H5_deencredit, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            OthersInfo_deencredit.getInstance_deencredit().setRpPath_deencredit(((JSONObject)obj).optString("url"));
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //获取还款H5（乌干达、肯尼亚）
    public void getOrderRepayH5_deencredit(FragmentActivity activity, String deferredFlag, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("deferredFlag", deferredFlag.trim());
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_GET_ORDER_REPAY_H5_deencredit, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            OthersInfo_deencredit.getInstance_deencredit().setRpPath_deencredit(((JSONObject)obj).optString("repayUrl"));
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //获取钱包列表（科特、喀麦隆、塞内加尔）
    public void requestAccountList_Fr_deencredit(FragmentActivity activity, Runnable respRunnable) {
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_FETCH_BOUND_BANK_ACCOUNT_deencredit, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONArray) {
                            AccountList_Fr_deencredit.parse_deencredit((JSONArray) obj);
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //新增钱包（科特、喀麦隆、塞内）
    public void addAccount_Fr_deencredit(FragmentActivity activity, String walletMobile, String walletMobileConfirm, String walletChannel, Runnable respRunnable, boolean addBindFlag) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("walletMobile", walletMobile);
            jsonObject.put("walletMobileConfirm", walletMobileConfirm);
            jsonObject.put("walletChannel", walletChannel);
            jsonObject.put("addBindFlag", addBindFlag ? "YES":"NO");
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_BIND_BANK_ACCOUNT_deencredit, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            JSONObject json = (JSONObject) obj;
                            OthersInfo_deencredit.getInstance_deencredit().setChannel_deencredit(json.optString("channel"));
                            OthersInfo_deencredit.getInstance_deencredit().setAddBindFlag_deencredit(json.optString("addBindFlag"));
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //删除钱包（科特、喀麦隆）
    public void deleteAccount_Fr_deencredit(FragmentActivity activity, String ids, Runnable respRunnable) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ids", ids);
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_DELETE_BOUND_BANK_ACCOUNT_deencredit, jsonObject),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    //获取评分弹窗信息
    public void requestStarWindowConfig_deencredit(FragmentActivity activity, Runnable respRunnable){
        requestWithLoadingDialog_deencredit(
                activity,
                getRequestBody_deencredit(Paths_deencredit.PATH_FETCH_STAR_WINDOW_CONFIG_deencredit, null),
                responseJson -> {
                    try {
                        int code = Integer.parseInt(responseJson.optString("code"));
                        String msg = responseJson.optString("msg");
                        Object obj = responseJson.opt("obj");
                        //失败响应
                        if (code!=0) {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                        //成功响应
                        else if (obj instanceof JSONObject) {
                            StarWindowConfig_deencredit.parseData_deencredit((JSONObject) obj);
                            safeDismissLoadingDialog_deencredit();
                            //...
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else safeDismissLoadingDialog_deencredit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        );
    }

    /**
     * 保存页面显示/隐藏打点
     */
    public void saveBuriedPoint_deencredit(String page, String event, String remark) {
        if (CountryConfig_deencredit.getInstance_deencredit().isFrontEmpty_deencredit()||TextUtils.isEmpty(page)) return;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("type", Constant_deencredit.PRODUCT_deencredit +"_"+page+"_"+event);
            if (!TextUtils.isEmpty(remark)) jsonObject.put("remark",remark);
        } catch (JSONException e) {
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }

        if (!CountryConfig_deencredit.getInstance_deencredit().isFrontEmpty_deencredit()){
            String commonPath = CountryConfig_deencredit.getInstance_deencredit().getCommonPath_deencredit();
            if (TextUtils.isEmpty(commonPath)) return;
            new Thread(() -> {

                Request request = new Request.Builder()
                        .url(commonPath)
                        .header("product", Constant_deencredit.PRODUCT_deencredit)
                        .post(RequestBody.Companion.create(getRequestBody_deencredit(Paths_deencredit.PATH_USER_BURIED_POINT_deencredit, jsonObject), MediaType.Companion.parse("*/*;charset=utf-8")))
                        .build();

                mClient_deencredit.newCall(request).enqueue(
                        //响应
                        new Callback() {
                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {}
                            @Override
                            public void onResponse(@NonNull Call call, @NonNull Response response) {}
                        }
                );

            }).start();
        }
    }

    /**
     * 获取目标地区path
     */
    public void requestTzConfig_deencredit(FragmentActivity activity, String tz, Runnable respRunnable){

        safeShowLoadingDialog_deencredit(activity);

        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("p", Constant_deencredit.PRODUCT_deencredit.toLowerCase());
            jsonObject.put("tz",tz);
            Log.w("---tz---", tz );
        }catch (JSONException e){
            e.printStackTrace();
            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
            safeDismissLoadingDialog_deencredit();
        }

        String body = encryptDefault_deencredit(jsonObject.toString());

        new Thread(() -> {
            MediaType mediaType = MediaType.Companion.parse("*/*;charset=utf-8");
            Request request = new Request.Builder()
                    .url(Paths_deencredit.TimezonePath_deencredit)
                    .header("product", Constant_deencredit.PRODUCT_deencredit)
                    .post(RequestBody.Companion.create(body, mediaType))
                    .build();
            Call call = mClient_deencredit.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_request_fail_deencredit));
                    safeDismissLoadingDialog_deencredit();
                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    String body = decryptDefault_deencredit(Objects.requireNonNull(response.body()).string());
                    Log.w("--tz_body--", body );
                    try {
                        JSONObject responseJson = new JSONObject(body);
                        safeDismissLoadingDialog_deencredit();
                        String msg = responseJson.optString("msg");
                        if (msg.equalsIgnoreCase("success")){
                            String[] data = responseJson.optString("data").split(",");
                            String front = "";
                            String mobileprefix = "";
                            if (data.length>0) front = data[0];
                            if (data.length>1) mobileprefix = data[1];
                            CountryConfig_deencredit.getInstance_deencredit().setFront_deencredit(front);
                            CountryConfig_deencredit.getInstance_deencredit().setAreaCode_deencredit(mobileprefix);
                            safeDismissLoadingDialog_deencredit();
                            if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                        }else {
                            safeDismissLoadingDialog_deencredit();
                            safeShowToast_deencredit(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_error_deencredit));
                        safeDismissLoadingDialog_deencredit();
                    }
                }
            });
        }).start();
    }

    /**
     * 上传图片
     */
    public void uploadImg_deencredit(FragmentActivity activity, String filePath_equitable, Runnable respRunnable) {
        safeShowLoadingDialog_deencredit(activity);
        try {
            if (!TextUtils.isEmpty(filePath_equitable)) {
                String fileSuffix_equitable = filePath_equitable.endsWith(".png") ? "image/png" : "image/jpg";
                final MediaType mediaType_equitable = MediaType.parse(fileSuffix_equitable);
                final File imageFile_equitable = new File(filePath_equitable);

                final String uplodPaths_equitable = Paths_deencredit.BasePath_deencredit + CountryConfig_deencredit.getInstance_deencredit().getFront_deencredit()+"/"+ PATH_UPLOAD_IMAGE_FILE_deencredit;

                if (imageFile_equitable.isFile()) {
                    RequestBody imageBody = RequestBody.create(mediaType_equitable, imageFile_equitable);

                    RequestBody requestBody_equitable = new MultipartBody.Builder()
                            .setType(MultipartBody.FORM)
                            .addFormDataPart("image", imageFile_equitable.getName(), imageBody)
                            .build();

                    Request request_equitable = new Request.Builder()
                            .url(uplodPaths_equitable)
                            .post(requestBody_equitable)
                            .build();
                    Call call = mClient_deencredit.newCall(request_equitable);
                    //响应
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                            safeShowToast_deencredit(BaseApplication_deencredit.getInstant().getString(R.string.tips_request_fail_deencredit));
                            safeDismissLoadingDialog_deencredit();
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            String body = Objects.requireNonNull(response.body()).string();
                            try {
                                JSONObject responseJson = new JSONObject(body);
                                int code = Integer.parseInt(responseJson.optString("code"));
                                String msg = responseJson.optString("msg");
                                Object obj = responseJson.opt("obj");
                                //失败响应
                                if (code!=0) {
                                    safeShowToast_deencredit(msg);
                                    return;
                                }
                                //成功响应
                                if (obj instanceof JSONObject) {
                                    OthersInfo_deencredit.getInstance_deencredit().setImgPath_deencredit(((JSONObject)obj).optString("url"));
                                    safeDismissLoadingDialog_deencredit();
                                    //...
                                    if (respRunnable!=null) safeRunUi_deencredit(respRunnable);
                                }else safeDismissLoadingDialog_deencredit();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
